package com.xw.unification.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonFactoryBuilder;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.util.TimeZone;

/**
 * @author qiwei
 */
public abstract class JacksonUtils {

    private static final String DATE_TIME_FORMAT_PATTERN = "yyyy-mm-dd HH:mm:ss";
    private static final JsonFactory DEFAULT_JSON_FACTORY;
    private static final ObjectMapper OM;

    static {
        DEFAULT_JSON_FACTORY = new JsonFactoryBuilder()
                .configure(JsonReadFeature.ALLOW_JAVA_COMMENTS, true)
                .configure(JsonReadFeature.ALLOW_MISSING_VALUES, true)
                .configure(JsonReadFeature.ALLOW_YAML_COMMENTS, true)
                .build();
        OM = generateObjectMapper();
        configure(OM);
        customizedConfigure(OM);
    }

    private static ObjectMapper generateObjectMapper() {
        return Jackson2ObjectMapperBuilder.json()
                .simpleDateFormat(DATE_TIME_FORMAT_PATTERN)
                .failOnEmptyBeans(false)
                .factory(DEFAULT_JSON_FACTORY)
                .build();
    }

    protected static void configure(ObjectMapper objectMapper) {
        objectMapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING, true);
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        objectMapper.setTimeZone(TimeZone.getTimeZone(TimeZone.getDefault().getID()));
    }

    public static String json(Object object) {
        try {
            if (object == null) {
                return null;
            }
            return OM.writeValueAsString(object);
        } catch (JsonProcessingException e) {
            return null;
        }
    }

    public static <T> T parse(String json, Class<T> type) {
        try {
            if (json == null) {
                return null;
            }
            return OM.readValue(json, type);
        } catch (JsonProcessingException e) {
            return null;
        }
    }

    /**
     * 允许自己传入 ObjectMapper
     * @param objectMapper
     */
    public static JacksonDelegate custom(ObjectMapper objectMapper) {
        if (objectMapper == null) {
            throw new IllegalArgumentException("ObjectMapper can not be null.");
        }
        return null;
    }

    private static void customizedConfigure(ObjectMapper objectMapper) {
        // 小写命名
        objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.LOWER_CAMEL_CASE);
        OM.setSerializationInclusion(JsonInclude.Include.NON_NULL);
    }

}
